﻿using System;
using System.Runtime.Remoting.Contexts;

namespace AspectIntercept
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
    public class InterceptAttribute : ContextAttribute
    {
        public Boolean InterceptAllMethods { get; set; }
        public Type PreProcesor { get; set; }
        public Type PostProcesor { get; set; }
        public String[] OmitMethods { get; set; }

        public InterceptAttribute()
            : base("Intercept")
        {
            InterceptAllMethods = false;
            PreProcesor = null;
            PostProcesor = null;
            OmitMethods = new string[] { };
        }

        public override void Freeze(Context newContext)
        {
        }

        public override void GetPropertiesForNewContext(System.Runtime.Remoting.Activation.IConstructionCallMessage ctorMsg)
        {
            ctorMsg.ContextProperties.Add(new InterceptProperty() { InterceptAllMethods = InterceptAllMethods, PostProcesor = PostProcesor, PreProcesor = PreProcesor, OmitMethods = OmitMethods });
        }

        public override bool IsContextOK(Context ctx, System.Runtime.Remoting.Activation.IConstructionCallMessage ctorMsg)
        {
            InterceptProperty p = ctx.GetProperty("Intercept") as InterceptProperty;
            if (p == null)
                return false;
            return true;
        }

        public override bool IsNewContextOK(Context newCtx)
        {
            InterceptProperty p = newCtx.GetProperty("Intercept") as InterceptProperty;
            if (p == null)
                return false;
            return true;
        }
    }
}
